mruby 4.0.0
mruby is the lightweight implementation of the Ruby language
Loading...
Searching...
No Matches
error.h
Go to the documentation of this file.
1
6
7#ifndef MRUBY_ERROR_H
8#define MRUBY_ERROR_H
9
10#include "common.h"
11#include <string.h>
12
17
18struct RException {
19 MRB_OBJECT_HEADER;
20 struct iv_tbl *iv;
21 struct RBasic *mesg; // NULL or probably RString
22 struct RBasic *backtrace; // NULL, RArray or RData
23};
24
25/* error that should terminate execution */
26#define MRB_EXC_EXIT 65536
27#define MRB_EXC_EXIT_P(e) ((e)->flags & MRB_EXC_EXIT)
28/* retrieve status value from exc; need <mruby/variable.h> and <mruby/presym.h> */
29#define MRB_EXC_EXIT_STATUS(mrb,e) ((int)mrb_as_int((mrb),mrb_obj_iv_get((mrb),(e),MRB_SYM(status))))
30/* exit with SystemExit status */
31#define MRB_EXC_CHECK_EXIT(mrb,e) do {if (MRB_EXC_EXIT_P(e)) exit(MRB_EXC_EXIT_STATUS((mrb),(e)));} while (0)
32
33#define mrb_exc_ptr(v) ((struct RException*)mrb_ptr(v))
34
35MRB_API mrb_noreturn void mrb_sys_fail(mrb_state *mrb, const char *mesg);
36MRB_API mrb_value mrb_exc_new_str(mrb_state *mrb, struct RClass* c, mrb_value str);
37#define mrb_exc_new_lit(mrb, c, lit) mrb_exc_new_str(mrb, c, mrb_str_new_lit(mrb, lit))
38MRB_API mrb_noreturn void mrb_no_method_error(mrb_state *mrb, mrb_sym id, mrb_value args, const char *fmt, ...);
39
40/* On 32-bit platforms whose ABI gives uint64_t/double 8-byte alignment
41 (ARM, MIPS, PowerPC, xtensa, ...), embedding mrb_value directly in
42 RBreak forces 8-byte alignment that pushes the struct past the 5-word
43 RVALUE budget via padding. Store the value bits as a uint32_t array
44 (4-byte aligned) to dodge that padding. Word-boxing's mrb_value is
45 just a uintptr_t with no over-alignment, and 64-bit platforms have no
46 alignment gap to begin with, so neither needs the workaround. */
47#if defined(MRB_64BIT) || defined(MRB_WORD_BOXING)
48#undef MRB_USE_RBREAK_VALUE_UNION
49#else
50#define MRB_USE_RBREAK_VALUE_UNION 1
51#endif
52
53/*
54 * flags:
55 * 0..7: enum mrb_vtype (only when MRB_USE_RBREAK_VALUE_UNION and
56 * !MRB_NAN_BOXING; nan-boxing encodes the type in the bits)
57 * 8..10: RBREAK_TAGs in src/vm.c (otherwise, set to 0)
58 */
59struct RBreak {
60 MRB_OBJECT_HEADER;
61 uintptr_t ci_break_index; // The top-level ci index to break. One before the return destination.
62#ifndef MRB_USE_RBREAK_VALUE_UNION
63 mrb_value val;
64#elif defined(MRB_NAN_BOXING)
65 /* nan-boxing: mrb_value is a single 64-bit word (type encoded in NaN bits) */
66 uint32_t value[sizeof(mrb_value) / sizeof(uint32_t)];
67#else
68 /* no-boxing: store only the union bits; tt goes in flags */
69 uint32_t value[sizeof(union mrb_value_union) / sizeof(uint32_t)];
70#endif
71};
72
73#ifndef MRB_USE_RBREAK_VALUE_UNION
74#define mrb_break_value_get(brk) ((brk)->val)
75#define mrb_break_value_set(brk, v) ((brk)->val = v)
76#elif defined(MRB_NAN_BOXING)
77static inline mrb_value
78mrb_break_value_get(struct RBreak *brk)
79{
80 mrb_value val;
81 memcpy(&val, brk->value, sizeof(val));
82 return val;
83}
84static inline void
85mrb_break_value_set(struct RBreak *brk, mrb_value val)
86{
87 memcpy(brk->value, &val, sizeof(val));
88}
89#else
90#define RBREAK_VALUE_TT_MASK ((1 << 8) - 1)
91static inline mrb_value
92mrb_break_value_get(struct RBreak *brk)
93{
94 mrb_value val;
95 memcpy(&val.value, brk->value, sizeof(val.value));
96 val.tt = (enum mrb_vtype)(brk->flags & RBREAK_VALUE_TT_MASK);
97 return val;
98}
99static inline void
100mrb_break_value_set(struct RBreak *brk, mrb_value val)
101{
102 memcpy(brk->value, &val.value, sizeof(val.value));
103 brk->flags &= ~RBREAK_VALUE_TT_MASK;
104 brk->flags |= val.tt;
105}
106#endif /* MRB_USE_RBREAK_VALUE_UNION */
107
112/* clear error status in the mrb_state structure */
114/* returns TRUE if error in the previous call; internally calls mrb_clear_error() */
115MRB_API mrb_bool mrb_check_error(mrb_state *mrb);
116
121typedef mrb_value mrb_protect_error_func(mrb_state *mrb, void *userdata);
122MRB_API mrb_value mrb_protect_error(mrb_state *mrb, mrb_protect_error_func *body, void *userdata, mrb_bool *error);
123
129MRB_API mrb_value mrb_protect(mrb_state *mrb, mrb_func_t body, mrb_value data, mrb_bool *state);
130
137 mrb_func_t ensure, mrb_value e_data);
138
145 mrb_func_t rescue, mrb_value r_data);
146
153 mrb_func_t rescue, mrb_value r_data,
154 mrb_int len, struct RClass **classes);
155
183#define MRB_ENSURE(mrb, result_var, func, data) \
184 for (mrb_bool MRB_UNIQNAME(_break_) = FALSE; \
185 !MRB_UNIQNAME(_break_) && \
186 (((result_var) = mrb_protect_error(mrb, func, data, &MRB_UNIQNAME(_break_))), \
187 ((mrb)->exc = (MRB_UNIQNAME(_break_) ? mrb_obj_ptr((result_var)) : NULL)), \
188 TRUE); \
189 (void)(MRB_UNIQNAME(_break_) && (mrb)->jmp && (mrb_exc_raise(mrb, result_var), TRUE)), \
190 MRB_UNIQNAME(_break_) = TRUE)
191
193
194#endif /* MRUBY_ERROR_H */
mruby Symbol.
mruby common platform definition"
#define MRB_END_DECL
End declarations in C mode.
Definition common.h:28
#define MRB_BEGIN_DECL
Start declarations in C mode.
Definition common.h:26
#define MRB_API
Declare a public mruby API function.
Definition common.h:108
#define mrb_noreturn
Shared compiler macros.
Definition common.h:50
void mrb_clear_error(mrb_state *mrb)
Error check.
Definition error.c:879
mrb_value mrb_protect(mrb_state *mrb, mrb_func_t body, mrb_value data, mrb_bool *state)
Protect (takes mrb_value for body argument).
mrb_value mrb_protect_error_func(mrb_state *mrb, void *userdata)
Protect.
Definition error.h:121
mrb_value mrb_protect_error(mrb_state *mrb, mrb_protect_error_func *body, void *userdata, mrb_bool *error)
Protects a C function call from mruby exceptions.
Definition vm.c:540
mrb_value mrb_rescue_exceptions(mrb_state *mrb, mrb_func_t body, mrb_value b_data, mrb_func_t rescue, mrb_value r_data, mrb_int len, struct RClass **classes)
Rescue exception.
mrb_value mrb_rescue(mrb_state *mrb, mrb_func_t body, mrb_value b_data, mrb_func_t rescue, mrb_value r_data)
Rescue.
mrb_value mrb_ensure(mrb_state *mrb, mrb_func_t body, mrb_value b_data, mrb_func_t ensure, mrb_value e_data)
Ensure.
mrb_value(* mrb_func_t)(mrb_state *mrb, mrb_value self)
Function pointer type for a function callable by mruby.
Definition mruby.h:227
String class
Definition object.h:19
Definition error.h:59
Class class.
Definition class.h:17
mruby error handling.
Definition error.h:18
Definition variable.c:16
Definition mruby.h:280
Definition boxing_nan.h:40
Definition boxing_no.h:15